package com.formerlyknownasprince.wheresmystuff.models;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import android.util.Log;


/**
 * @author      Tim Swihart 		<email obscured since this file is public>
 * @version     0.3                 
 * @since       2013-03-04
 */
public class Item extends ServerObject {
	protected static String baseURL = "/items/";
	
	@JsonProperty("name")
	private String name;
	
	@JsonProperty("reward")
	private double reward;
	
	@JsonProperty("tags")
	private List<Tag> tags;
	
	@JsonProperty("found")
	private Boolean found;
	
	@JsonProperty("archived")
	private Boolean archived;
	
	@JsonProperty("description")
	private String description;
	
	@JsonProperty("date")
	private String date;
	
	//private String authId;
	
	public Item() {
		this.setName("");
	}
	
	public Item(String name, String description, Double reward, Boolean found) throws ClientProtocolException, IOException {
		this.setName(name);
		this.setDescription(description);
		this.setReward(reward);
		this.setFound(found);
		this.setArchived(false);
		this.setDate(""); // TODO: get the date set properly
		this.om = new ObjectMapper();
		try {
			this.create();
		} catch (SyncException se) {
			se.printStackTrace();
		}
	}
	
	public Item(JsonNode usr) {
		if (this.om == null) this.om = new ObjectMapper();
		try {
			Item u = om.treeToValue(usr, Item.class);
			this.setName(u.getName());
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public Item(String json) {
		this.om = new ObjectMapper();
		try {
			Item tempItem = om.readValue(json, Item.class);
			this.setName(tempItem.getName());
			this.setReward(tempItem.getReward());
			this.setArchived(false);
			this.setFound(tempItem.getFound());
			this.setDescription(tempItem.getDescription());
			//this.setTags(tempItem.getTags());
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//Static methods
	
	/**
	 * TODO: implement
	 * @param date
	 * @return
	 */
	public static Item findByDate(String date) {
		return new Item();
	}
	
	/**
	 * TODO: implement
	 * @param tags
	 * @return
	 */
	public static Item findByTags(List<Tag> tags) {
		return new Item();
	}
	
	public void addTag(Tag t) {
		if (this.tags == null) {
			this.tags = new ArrayList<Tag>();
		}
		if (!this.tags.contains(t)) {
			this.tags.add(t);
		}
	}
	
	//Update and management methods
	
	public void removeTag(Tag t) {
		this.tags.remove(t);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getReward() {
		return reward;
	}

	public void setReward(double reward) {
		this.reward = reward;
	}

	public List<Tag> getTags() {
		return tags;
	}

	public void setTags(List<Tag> tags) {
		this.tags = tags;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	
	public String toString() {
		return "This is an item";
	}
	
	public JsonNode toJson() {
		return om.valueToTree(this);
	}

	public Boolean getFound() {
		return found;
	}

	public void setFound(Boolean found) {
		this.found = found;
	}

	public Boolean getArchived() {
		return archived;
	}

	public void setArchived(Boolean archived) {
		this.archived = archived;
	}
	
	@Override
	public String getBaseURL() {
		return this.baseURL;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}
}
